/*   1:    */ package com.jetbrains.licenseService.util;
/*   2:    */ 
/*   3:    */

import javax.naming.NamingException;
import javax.naming.directory.Attributes;
import javax.naming.directory.InitialDirContext;
import java.net.*;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/*   4:    */
/*   5:    */
/*   6:    */
/*   7:    */
/*   8:    */
/*   9:    */
/*  10:    */
/*  11:    */
/*  12:    */
/*  13:    */
/*  14:    */
/*  15:    */
/*  16:    */
/*  17:    */
/*  18:    */
/*  19:    */
/*  20:    */
/*  21:    */ 
/*  22:    */ public class DnsUtil
/*  23:    */ {
/*  24:    */   public static final String DEFAULT_DNS_ATTRIBUTE_NAME_PREFIX = "_jetbrains-license-server";
/*  25:    */   public static final String DNS_ATTRIBUTE_NAME_PREFIX_PROPERTY = "dns.attribute.name.prefix";
/*  26:    */   
/*  27:    */   static String calcHostName()
/*  28:    */   {
/*  29:    */     try
/*  30:    */     {
/*  31: 17 */       return a();
/*  32:    */     }
/*  33:    */     catch (Throwable localThrowable) {}
/*  34: 20 */     return "<unknown>";
/*  35:    */   }
/*  36:    */   
/*  37:    */   public static String detectServerUrl()
/*  38:    */   {
/*  39:    */     try
/*  40:    */     {
/*  41: 26 */       return (String)c(b()).get("url");
/*  42:    */     }
/*  43:    */     catch (Exception localException) {}
/*  44: 29 */     return "http://jetbrains-license-server";
/*  45:    */   }
/*  46:    */   
/*  47:    */   private static void a(String paramString, Processor<String> paramProcessor)
/*  48:    */     throws UnknownHostException
/*  49:    */   {
/*  50: 34 */     HashSet localHashSet = new HashSet();
/*  51:    */     Object localObject1;
/*  52:    */     Object localObject2;
/*  53:    */     try
/*  54:    */     {
/*  55: 37 */       Enumeration localEnumeration = NetworkInterface.getNetworkInterfaces();
/*  56: 38 */       while (localEnumeration.hasMoreElements())
/*  57:    */       {
/*  58: 39 */         localObject1 = (NetworkInterface)localEnumeration.nextElement();
/*  59: 40 */         localObject2 = ((NetworkInterface)localObject1).getInetAddresses();
/*  60: 41 */         while (((Enumeration)localObject2).hasMoreElements())
/*  61:    */         {
/*  62: 42 */           InetAddress localInetAddress1 = (InetAddress)((Enumeration)localObject2).nextElement();
/*  63: 43 */           if (!a(localInetAddress1, localHashSet, paramProcessor)) {
/*  64: 44 */             return;
/*  65:    */           }
/*  66:    */         }
/*  67:    */       }
/*  68:    */     }
/*  69:    */     catch (SocketException localSocketException) {}
/*  70: 51 */     if (paramString != null)
/*  71:    */     {
/*  72: 52 */       String[] arrayOfString = paramString.split(" ");
/*  73: 53 */       localObject1 = Pattern.compile("dns://(\\d+\\.\\d+\\.\\d+\\.\\d+)");
/*  74: 54 */       for (CharSequence localCharSequence : arrayOfString)
/*  75:    */       {
/*  76: 55 */         Matcher localMatcher = ((Pattern)localObject1).matcher(localCharSequence);
/*  77: 56 */         if (localMatcher.matches())
/*  78:    */         {
/*  79: 57 */           InetAddress localInetAddress2 = InetAddress.getByName(localMatcher.group(1));
/*  80: 58 */           if (!a(localInetAddress2, localHashSet, paramProcessor)) {
/*  81: 59 */             return;
/*  82:    */           }
/*  83:    */         }
/*  84:    */       }
/*  85:    */     }
/*  86: 65 */     a(InetAddress.getLocalHost(), localHashSet, paramProcessor);
/*  87:    */   }
/*  88:    */   
/*  89:    */   private static boolean a(InetAddress paramInetAddress, Collection<String> paramCollection, Processor<String> paramProcessor)
/*  90:    */   {
/*  91: 69 */     if ((paramInetAddress.isMulticastAddress()) || ((paramInetAddress instanceof Inet6Address)) || (paramInetAddress.isLoopbackAddress())) {
/*  92: 70 */       return true;
/*  93:    */     }
/*  94: 73 */     Object localObject = paramInetAddress.getCanonicalHostName();
/*  95:    */     for (;;)
/*  96:    */     {
/*  97: 75 */       String str = b((String)localObject);
/*  98: 76 */       if (!a(str)) {
/*  99: 77 */         return true;
/* 100:    */       }
/* 101: 79 */       if (!paramCollection.add(str)) {
/* 102: 80 */         return true;
/* 103:    */       }
/* 104: 82 */       if (!paramProcessor.process(str)) {
/* 105: 82 */         return false;
/* 106:    */       }
/* 107: 83 */       localObject = str;
/* 108:    */     }
/* 109:    */   }
/* 110:    */   
/* 111:    */   private static boolean a(String paramString)
/* 112:    */   {
/* 113: 88 */     for (int i = 0; i < paramString.length(); i++) {
/* 114: 89 */       if (Character.isLetter(paramString.charAt(i))) {
/* 115: 89 */         return true;
/* 116:    */       }
/* 117:    */     }
/* 118: 91 */     return false;
/* 119:    */   }
/* 120:    */   
/* 121:    */   private static String b(String paramString)
/* 122:    */   {
/* 123: 95 */     int i = paramString.indexOf('.');
/* 124: 96 */     return i > 0 ? paramString.substring(i + 1) : "";
/* 125:    */   }
/* 126:    */   
/* 127:    */   private static String a()
/* 128:    */     throws UnknownHostException
/* 129:    */   {
/* 130:100 */     return InetAddress.getLocalHost().getCanonicalHostName();
/* 131:    */   }
/* 132:    */   
/* 133:    */   private static String b()
/* 134:    */     throws NamingException, UnknownHostException
/* 135:    */   {
/* 136:104 */     Hashtable localHashtable = new Hashtable();
/* 137:105 */     localHashtable.put("java.naming.factory.initial", "com.sun.jndi.dns.DnsContextFactory");
/* 138:106 */     InitialDirContext localInitialDirContext = new InitialDirContext(localHashtable);
/* 139:107 */     final String str1 = System.getProperty("dns.attribute.name.prefix", "_jetbrains-license-server");
/* 140:108 */     String str2 = (String)localInitialDirContext.getEnvironment().get("java.naming.provider.url");
/* 141:    */     
/* 142:110 */     final String[] arrayOfString = { null };
/* 143:111 */     Processor local1 = new Processor()
/* 144:    */     {
/* 145:    */       public boolean process(String paramAnonymousString)
/* 146:    */       {
/* 147:    */         try
/* 148:    */         {
/* 149:114 */           Attributes localAttributes = this.val$context.getAttributes(str1 + "." + paramAnonymousString);
/* 150:115 */           arrayOfString[0] = ((String)localAttributes.get("TXT").get());
/* 151:116 */           return false;
/* 152:    */         }
/* 153:    */         catch (Exception localException) {}
/* 154:119 */         return true;
/* 155:    */       }
/* 156:122 */     };
/* 157:123 */     a(str2, local1);
/* 158:    */     
/* 159:125 */     String str3 = arrayOfString[0];
/* 160:126 */     if (str3 != null) {
/* 161:127 */       return str3;
/* 162:    */     }
/* 163:130 */     throw new NamingException("No license server DNS entries detected in any scanned local domains");
/* 164:    */   }
/* 165:    */   
/* 166:    */   private static Map<String, String> c(String paramString)
/* 167:    */   {
/* 168:134 */     HashMap localHashMap = new HashMap();
/* 169:    */     
/* 170:136 */     StringTokenizer localStringTokenizer = new StringTokenizer(paramString, ";", false);
/* 171:137 */     while (localStringTokenizer.hasMoreTokens())
/* 172:    */     {
/* 173:138 */       String str1 = localStringTokenizer.nextToken();
/* 174:139 */       int i = str1.indexOf('=');
/* 175:140 */       if (i > 0)
/* 176:    */       {
/* 177:141 */         String str2 = str1.substring(0, i).trim();
/* 178:142 */         String str3 = str1.substring(i + 1).trim();
/* 179:143 */         localHashMap.put(str2, str3);
/* 180:    */       }
/* 181:    */     }
/* 182:147 */     return localHashMap;
/* 183:    */   }
/* 184:    */   
/* 185:    */   static abstract interface Processor<T>
/* 186:    */   {
/* 187:    */     public abstract boolean process(T paramT);
/* 188:    */   }
/* 189:    */ }



/* Location:           E:\idea\

 * Qualified Name:     com.jetbrains.licenseService.util.DnsUtil

 * JD-Core Version:    0.7.0.1

 */